package com.example.mydemo.utils;

import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.tomcat.util.security.MD5Encoder;

import javax.activation.MailcapCommandMap;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.UUID;

/**
 * @author tangpengcheng
 * @description：天翼云直播解密
 * @date 2020-10-29 14:20
 */
public class Tianyiyun {

    /**
     * 获得防盗链播放地址
     */
    public static String liveEncode(URL url, String key){
        if(url == null){
            return "";
        }

        String hex = unixTimestamp2Hex();

        StringBuffer sb = new StringBuffer();

        sb.append(url.getProtocol()).append("://").append(url.getHost()).append(url.getPath());
        sb.append("?sign=").append(MD5.md5(key + url.getPath() + hex));
        sb.append("&t=").append(hex);
        return sb.toString();
    }

    protected final static String unixTimestamp2Hex(){
        long timestamp = System.currentTimeMillis();
        return Long.toHexString(timestamp);
    }

    public static void main(String[] args) {

        try {
            System.out.println(liveEncode(new URL("http://pull-hls.live.rmtyun.cn/media/NaW5e67696fa4161.m3u8"),"NZLm0E0GXhjTlxg2LJLk"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}
